+(function() {
	/**
	 * 1. 初始化棋盘（或读取缓存
	 * 2. 确定一步哪一方下棋
	 * 3. 提示可能赢的下棋方式
	 * 4. 下棋
	 */
	class Tictactoe {
		/**
		 * 初始化棋盘
		 * @return {[type]} [description]
		 */
		init (cacheBoardKey, cachePlayerKey) {
			this.size = 3
			this.blank = 'e'

			// 获取缓存中的棋盘状态
			if (!!cacheBoardKey && !!cachePlayerKey) {
				let cacheBoard = window.localStorage.getItem(cacheBoardKey)
				let player = window.localStorage.getItem(cachePlayerKey)
				if (!!cacheBoard) {
					this.board = JSON.parse(cacheBoard);
				}

				if (!!player) {
					this.setPlayer(player);
				}
			}

			this.board = this.board || [
				['e', 'e', 'e'],
				['e', 'e', 'e'],
				['e', 'e', 'e'],
			]
		}

		setPlayer (player) {
			this.currPlayer = player
		}

		/**
		 * 计算可能取胜的下棋方式
		 */
		getPossibleSucc (key, curBoard) {
			let ret = []

			for (let i = 0; i < this.size; i ++ ) {
				for (let j = 0; j < this.size; j ++) {
					if (this.getResultOnPoint(i, j, key, curBoard)) {
						ret.push([i, j])
					}
				}
			}

			console.log(JSON.stringify(ret))
			return ret;
		}

		/**
		 * 更新棋盘，计算下棋结果（是否赢）
		 * @param  {int} x 行
		 * @param  {int} y 列
		 * @return {boolean}   返回下棋的结果
		 */
		updateBoard (x, y) {
			let ret = this.getResultOnPoint(x, y)
			// 更新棋盘
			this.board[x][y] = this.player;

			// 设置选手
			this.setPlayer(this.player === 'x'?'o':'x')

			return ret
		}

		/**
		 * 获取某位置上下棋的结果
		 * @param  {int} x 行
		 * @param  {int} y 列
		 * @return {boolean}   返回该位置上的结果
		 */
		getResultOnPoint(x, y, key, board) {
			board = board || this.board
			key = key || this.player

			let ret = false

			if (board[x][y] === this.blank) {
				// 仅为空位才可能下子
				let xLine = true, yLine = true;
				for (let i = 0; i < this.size; i ++ ) {
					// 横向
					if (i != x && key != board[i][y]) {
						xLine = false;
					}
					// 纵向
					if (i != y && key != board[x][i]) {
						yLine = false;
					}
				}

				ret = xLine || yLine
			}

			return ret
		}
	}

	class Drawer {
		done (dom, board) {
			
		}
	}

	let tictactoe = new Tictactoe(),
		board = [
				['x', 'e', 'e'],
				['e', 'x', 'x'],
				['e', 'x', 'e'],
			]
	// 初始化
	tictactoe.init()
	// 获取可能获胜的下一步位置
	tictactoe.getPossibleSucc('x', board)
}())